package com.cwvs.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.cwvs.helper.UrlHelper;
import com.cwvs.utils.MySqlPageUtil;
import com.cwvs.utils.Pages;

/**
 * 
 * 首页广告位相关DAO
 */
@Repository
public class AppAdvertDao {
	@Resource
	private JdbcTemplate jdbcTemplate;
	@Resource
	private MySqlPageUtil mySqlPageUtil;
	@Resource
	private UrlHelper urlHelper;

	/**
	 * 首页广告位
	 * 
	 * @return
	 */
	public List<Map<String, Object>> advertList() {
		List<Map<String, Object>> ls= jdbcTemplate
				.queryForList("SELECT PIC_URL,NAME FROM APP_ADVERT WHERE IS_ACTIVE=1 AND STIME<=NOW() AND ETIME>NOW() ORDER BY SORT_NUM ASC limit 0, 5 ");
		for(Map<String,Object> map:ls){
			map.put("PIC_URL", urlHelper.img01UrlRootWithPort(MapUtils.getString(map, "PIC_URL")));
		}
		return ls;
	}
	/**
	 * 广告列表
	 * 
	 * @return
	 */
	public Pages advertList(Map<String, Object> m, Pages page) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT T1.ID,T1.PIC_URL,T1.NAME,T1.STIME ");
		sql.append(",T1.ETIME,T1.IS_ACTIVE,T1.SORT_NUM ");
		sql.append(" FROM APP_ADVERT T1 WHERE 1=1   ");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotBlank(MapUtils.getString(m, "NAME"))){
			sql.append(" AND UPPER(T1.NAME) LIKE ? ");
			params.add("%"+MapUtils.getString(m, "NAME")+"%");
		}
		sql.append(" ORDER BY T1.IS_ACTIVE DESC, T1.ETIME DESC ");
		String page_sql = mySqlPageUtil.getPageSql(page, sql.toString());
		List<Map<String, Object>> ls = jdbcTemplate.queryForList(page_sql,
				params.toArray());
		for(Map<String,Object> map:ls){
			map.put("PIC_URL", urlHelper.img01UrlRootWithPort(MapUtils.getString(map, "PIC_URL")));
		}
		page.setPage(ls);
		page.setTotalRecord(advertCount(m));
		return page;
	}
	private int advertCount(Map<String,Object> m){
		StringBuffer sql=new StringBuffer();
		sql.append(" SELECT COUNT(1) FROM APP_ADVERT T1 WHERE 1=1 ");
		List<Object> params = new ArrayList<Object>();
		if(StringUtils.isNotBlank(MapUtils.getString(m, "NAME"))){
			sql.append(" AND UPPER(T1.NAME) LIKE ? ");
			params.add("%"+MapUtils.getString(m, "NAME")+"%");
		}
		return jdbcTemplate.queryForInt(sql.toString(),params.toArray());
	}
	
	/**
	 * 保存或者修改广告
	 */
	public boolean saveOrUpdate(Map<String, Object> o) {
		if (StringUtils.isBlank(MapUtils.getString(o, "ID"))) {
			StringBuffer sbf = new StringBuffer();
			String id = UUID.randomUUID().toString();
			sbf.append(" INSERT INTO APP_ADVERT(");
			sbf.append("ID,PIC_URL,NAME ,STIME,ETIME,SORT_NUM)VALUES(");
			sbf.append("?,?,?,?,?,?)");
			List<Object> ls = new ArrayList<Object>();
			ls.add(id);
			ls.add(MapUtils.getString(o, "PIC_URL"));
			ls.add(MapUtils.getString(o, "NAME"));
			ls.add(MapUtils.getObject(o, "STIME",Date.class));
			ls.add(MapUtils.getObject(o, "ETIME",Date.class));
			ls.add(MapUtils.getIntValue(o, "SORT_NUM"));
			return jdbcTemplate.update(sbf.toString(), ls.toArray()) > 0;
		} else {
			StringBuffer sbf = new StringBuffer();
			sbf.append(" UPDATE APP_ADVERT SET PIC_URL=?,NAME=? ");
			sbf.append(" ,STIME=? ,ETIME=? ,SORT_NUM= ?");
			sbf.append(" WHERE ID=?  ");
			List<Object> ls = new ArrayList<Object>();
			ls.add(MapUtils.getString(o, "PIC_URL"));
			ls.add(MapUtils.getString(o, "NAME"));
			ls.add(MapUtils.getObject(o, "STIME",Date.class));
			ls.add(MapUtils.getObject(o, "ETIME",Date.class));
			ls.add(MapUtils.getIntValue(o, "SORT_NUM"));
			ls.add(MapUtils.getString(o, "ID"));
			return jdbcTemplate.update(sbf.toString(), ls.toArray()) > 0;
		}

	}
	
	/**
	 * 状态更新
	 * @param id
	 * @return
	 */
	public boolean advertSetStaus(String id,boolean status){
		return jdbcTemplate.update("update app_advert set is_active=? where id=? ",new Object[]{status,id})>0;
	}
	/**
	 * 更新排序号
	 * @param id
	 * @return
	 */
	public boolean advertSortNum(String id,int sort){
		return jdbcTemplate.update("update app_advert set sort_num=? where id=? ",new Object[]{sort,id})>0;
	}

}
